package com.aaa.cars.service.impl;

import com.aaa.cars.dao.CarDao;
import com.aaa.cars.dao.CarInsuranceDao;
import com.aaa.cars.entity.Car;
import com.aaa.cars.entity.CarInsurance;
import com.aaa.cars.entity.CarType;
import com.aaa.cars.service.CarInsuranceService;
import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnStatus;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (CarInsurance)表服务实现类
 *
 * @author makejava
 * @since 2020-12-10 11:25:32
 */
@Service("carInsuranceService")
public class CarInsuranceServiceImpl implements CarInsuranceService {
    @Resource
    private CarInsuranceDao carInsuranceDao;

    @Resource
    private CarDao carDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CarInsurance queryById(Integer id) {
        return this.carInsuranceDao.queryById(id);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param pageNo
     * @param pageSize
     * @param carInsurance 实例对象
     * @return 对象列表
     */
    @Override
    public Result queryAll(int pageNo, int pageSize, CarInsurance carInsurance) {
        if(pageNo==0||pageSize==0){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        //分页
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<CarInsurance> pageInfo = new PageInfo<CarInsurance>(carInsuranceDao.queryAll(carInsurance));
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), pageInfo);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<CarInsurance> queryAllByLimit(int offset, int limit) {
        return this.carInsuranceDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param carInsurance 实例对象
     * @return 实例对象
     */
    @Override
    public CarInsurance insert(CarInsurance carInsurance) {
        carInsurance.setType("车险");
        carInsurance.setBuyDate(new Date());
        carInsurance.setChassisNumber("1111");
        carInsurance.setEngineNumber("123456");
        carInsurance.setSeats(1);
        int result = this.carInsuranceDao.insert(carInsurance);
        if (result == 1) {
            CarInsurance carInsurance1 = new CarInsurance();
            carInsurance1.setNumber(carInsurance.getNumber());
            List<CarInsurance> carInsurances = carInsuranceDao.queryAll(carInsurance1);
            Integer id = carInsurances.get(carInsurances.size() - 1).getId();
            Integer number = carInsurances.get(carInsurances.size() - 1).getNumber();
            Car car = new Car();
            car.setId(number);
            car.setInsuranceId(id);
            carDao.update(car);
        }
        return carInsurance;
    }

    /**
     * 修改数据
     *
     * @param carInsurance 实例对象
     * @return 实例对象
     */
    @Override
    public CarInsurance update(CarInsurance carInsurance) {
        this.carInsuranceDao.update(carInsurance);
        return this.queryById(carInsurance.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.carInsuranceDao.deleteById(id) > 0;
    }
}